<?php
    include 'header.php';
?>
<form method="post" action="login.php"> 
<div id="loginForm">
<h2>Identifiez-vous</h2> 
<p>
	<span><strong>Identifiant :</strong></span> 
	<input class="text2" id="username" name="username" value="" type="text">
</p>
<p>
	<span><strong>Mot de passe :</strong></span> 
	<input class="text2" id="password" name="password" value="" type="password">
</p>
<div id="login_button">
	<input type="reset" value="Annuler">
	<input type="submit" value="Valider">
</div>
</div>
</form>
<script type="text/javascript" language="javacript"> 
$(document).ready(function(){
	$( "#txtDtCroix" ).datepicker({ dateFormat: 'yy-mm-dd' });
	$( "input:submit, input:reset" ).button();
});
</script>
<?php
	if(isset($_POST['username']) && isset($_POST['password']))
	{
		$requete = "SELECT * FROM utilisateur where username='".$_POST['username']."' and password='".$_POST['password']."';";
		$resultat = mysql_query($requete) or die ('Erreur '.$requete.' '.mysql_error());
		
		if($ligne = mysql_fetch_assoc($resultat)) 
		{
			$_SESSION['currentUser_username'] = $ligne["username"];
			$_SESSION['currentUser_Nom'] = $ligne["nom"];
			$_SESSION['currentUser_Prenom'] = $ligne["prenom"];
			$_SESSION['currentUser_isglobaladmin'] = $ligne["isglobaladmin"];
			$_SESSION['currentUser_id'] = $ligne["id"];
			
			$listePrjAdmin = array();
			$listePrjAdminName = array();
			
			if ($ligne["isglobaladmin"]==1)
			{
				$requetePrj = "SELECT * FROM projet p";
			}
			else
			{
				$requetePrj = "SELECT * FROM projet p, utilisateur_projet up 
								where p.id = up.id_projet
								and up.id_utilisateur = '".$ligne["id"]."'
								and isadmin=1";
			}
			
			$resultatPrj = mysql_query($requetePrj) or die ('Erreur '.$requetePrj.' '.mysql_error());
			while ($lignePrj = mysql_fetch_assoc($resultatPrj)){
				$listePrjAdmin[] = $lignePrj[id];
				$listePrjAdminName [] = $lignePrj[nom];
			}
			
			$_SESSION['currentUser_listePrjAdmin'] = $listePrjAdmin;
			$_SESSION['currentUser_listePrjAdminName'] = $listePrjAdminName;
			
			header("Location: ListeCroix.php");
		}
		else
		{
			echo "<span class='errorMsg'>Utilisateur inconnu.</span>";
		}
	}
?>
<?php
     include 'footer.php';
?>
